package com.lepu.app.fun.tool.ymsjb.adapter;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.config.UserConfig;
import com.app.utils.Const;
import com.app.utils.DateUtil;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.ymsjb.bean.BeanToolYiMiao;
public class ToolYiMiaoAdapter extends BaseAdapter {
	private LayoutInflater mLayoutInflater = null;
	public Context mContext = null;
	public ArrayList<BeanToolYiMiao> mArrayList = null;

	public ToolYiMiaoAdapter(Context context,ArrayList<BeanToolYiMiao> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			
		    convertView = mLayoutInflater.inflate(R.layout.tool_yimiao_item, null);

			holder = new ViewHolder();

			RelativeLayout itemLayout = (RelativeLayout) convertView.findViewById(R.id.itemLayout);
			TextView timeTextView = (TextView) convertView.findViewById(R.id.timeText);
			TextView nameTextView = (TextView) convertView.findViewById(R.id.nameText);
			TextView numberTextView = (TextView) convertView.findViewById(R.id.numberText);
			TextView noteTextView = (TextView) convertView.findViewById(R.id.noteText);
			ImageView addImageView = (ImageView) convertView.findViewById(R.id.addImage);

			holder.itemLayout = itemLayout;
			holder.timeTextView = timeTextView;
			holder.nameTextView = nameTextView;
			holder.numberTextView = numberTextView;
			holder.noteTextView = noteTextView;
			holder.addImageView = addImageView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanToolYiMiao item = mArrayList.get(position);
		
		
		
		holder.nameTextView.setText(item.name);
		holder.numberTextView.setText(item.number);
		holder.noteTextView.setText(item.note);
		
		//
		String babyDateS = UserConfig.getConfigString(mContext, Const.CONFIG_YIMIAO_BABY_DATE, "");
		
		long babyDateL = DateUtil.getLongTime(babyDateS);  
		
		babyDateL = babyDateL + (Integer.valueOf(item.time)*DateUtil.MONTH_MILLIS);
		
		holder.timeTextView.setText(DateUtil.getStringTime(babyDateL));
		
		//
		if(position > 0 && item.time.equals(mArrayList.get(position-1).time)){
			holder.timeTextView.setVisibility(View.GONE);
		}else{
			holder.timeTextView.setVisibility(View.VISIBLE);
		}
		
		//
		if(item.done == 0){
			holder.addImageView.setBackgroundResource(R.drawable.tool_daichan_not_icon);
		}else if(item.done == 1){
			holder.addImageView.setBackgroundResource(R.drawable.tool_daichan_has_icon);
		}
		
		holder.addImageView.setOnClickListener(new MyViewOnclicklistener(item,holder.addImageView));
		
		// 长按监听
		holder.itemLayout.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// 必须空实现，否则外部长按监听不起作用
				return false;
			}
		});
		
		return convertView;
	}
	
	 class MyViewOnclicklistener implements View.OnClickListener {
	        private BeanToolYiMiao bean;
	        private ImageView imageView;

	        public MyViewOnclicklistener(BeanToolYiMiao alarm, ImageView imageView) {
	            this.bean = alarm;
	            this.imageView = imageView;
	        }

	        @Override
	        public void onClick(View v) {
	            final int rid = v.getId();
	            switch (rid) {
	                case R.id.addImage: 
	                    if(bean.done == 0){ 
	                    	bean.done = 1;
	                    	if(BeanToolYiMiao.update(bean)){
	                    		imageView.setBackgroundResource(R.drawable.tool_daichan_has_icon);
	                    	}
	                    	
	                    	
	                    }else{  
	                    	bean.done = 0;
	                    	if(BeanToolYiMiao.update(bean)){
	                    		imageView.setBackgroundResource(R.drawable.tool_daichan_not_icon);
	                    	}
	                    }
	                	
	                    break;
	                default:
	                    break;
	            }
	        }
	    }
	


	private static class ViewHolder {
		RelativeLayout itemLayout;
		TextView timeTextView;
		TextView nameTextView;
		TextView numberTextView;
		TextView noteTextView;
		ImageView addImageView;
	}
}
